<template>
  <div class="login">
    <div class="logoDic">
      <div class="title">    <h3 >盐池零工市场服务平台</h3></div>
      <div class="logBox">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <h1>欢迎登录</h1>
      
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon
                slot="prefix"
                icon-class="user"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="password"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <div class="checkbox">
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            <div></div>
          </div>

          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <!-- <div @click="dengl"> 测试统一登录</div> -->
            <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'"
                >立即注册</router-link
              >
            </div>
          </el-form-item>
        </el-form>

        <div class="iconImg">
          <img
            src="@/assets/images/zhuti.png"
            alt=""
            style="width: 100%; height: 100%"
          />
        </div>
        <!--  底部  -->
        <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div> -->
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import { decrypt, encrypt } from "@/utils/jsencrypt";
import Cookies from "js-cookie";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
        console.log("参数", route);
      },
      immediate: true,
    },
  },
  created() {
    // this.dengl();
    // this.getCookie();
  },
  methods: {
    dengl() {
      const params = new URLSearchParams({
        grant_type: "authorization_code",
        response_type: "code",
        client_id: "4de8e00428f1441886f899812cda50ac",
        state: "state",
        redirect_uri: "https://farm.nxnycy.cn",
      });
      // 跳转到目标 URL
      window.location.href =
        "https://auth2.nxeduyun.com/oauth2/v1/connect/authorize?" +
        params.toString();
    },
    //获取验证码
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then((res) => {
              // this.$router.push({path:"/HelloWorld"})
              // return
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/loginbei.png");
  background-size: 100% 100%;
  // background: -webkit-linear-gradient(top, #99ccff, #c2e0ff, #fff);
}

.logoDic{
    width: 55%;
  height: 560px;
  background-color: #fff;
  border-radius: 50px;
}
.title {
  width: 100%;
  margin: 0px auto 30px auto;
  text-align: center;
   color: #498bff;
   font-size: 25px;
}
.logBox {
  display: flex;
  // background-color: pink;
  width: 98%;
  margin: 0 auto;
  // height: 560px;
  // background-color: #fff;
  // border-radius: 50px;
}
.iconImg {
  width: 60%;
  height: 80%;
  padding: 10px 10px;
  margin: auto 0;
}
.login-form {
  width: 33%;
  height: 55%;
  margin: auto;
  text-align: center;
  color: #498bff;
  font-weight: 600;
  h3 {
    color: #498bff;
    font-weight: 600;
  }
  opacity: 1;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  .checkbox {
    display: flex;
    justify-content: space-between;
    margin: 0px 0px 25px 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
